package 力扣._543_二叉树的直径;

import 力扣.公用数据结构.TreeNode;

public class Solution3 {


    //当前子树最大深度
    int max_d;

    int maxdeepth(TreeNode root) {
        if (root == null)
            return 0;
        int left = maxdeepth(root.left);
        int right = maxdeepth(root.right);
        max_d = Math.max(max_d, left + right);  //当前节点的最大直径
        return Math.max(left, right) + 1;

    }

    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null)
            return 0;
        maxdeepth(root);
        return max_d;
    }
}
